package com.yc.demo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "MathServlet", value = "/MathServlet")
public class MathServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String a = request.getParameter("a");
        final String b = request.getParameter("b");
        int ia,ib;
        response.setContentType("text/html;charset=utf-8");
        try {
           ia = Integer.valueOf(a);
        }
        catch (NumberFormatException e){
            response.getWriter().print("请输入正确的 a 的参数");
            return;
        }
        try {
            ib = Integer.valueOf(b);
        }
        catch (NumberFormatException e){
            response.getWriter().print("请输入正确的 b 的参数");
            return;
        }

        String op = request.getParameter("op");
        if ("+".equals(op)){
            response.getWriter().print(ia + ib);
        }else if ("-".equals(op)){
            response.getWriter().print(ia - ib);
        }else if ("*".equals(op)){
            response.getWriter().print(ia * ib);
        }else if ("/".equals(op)){
            if (ib == 0){
                response.getWriter().print("除数不能为0");
            }else {
                response.getWriter().print(ia / ib);
            }
        }

        response.getWriter().println(ia + ib);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
